package com.knife.tools;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Util
{
  public static String toUTF8(String str)
  {
    try
    {
      return new String(str.getBytes("ISO-8859-1"), "UTF-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace(); }
    return null;
  }

  public static String toGBK(String str)
  {
    try
    {
      return new String(str.getBytes("UTF-8"), "GBK");
    }
    catch (UnsupportedEncodingException e)
    {
      e.printStackTrace(); }
    return null;
  }

  public static String getSunday()
  {
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    c.set(7, 1);
    return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  }
  
  /**
 * @return 取得当前日期所在周的第一天
 */
public static String getMonday()
  {
    Calendar c = Calendar.getInstance();
    c.setTime(new Date());
    Date date=c.getTime();//当前日期
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.setTime(date);
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
    return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  }

  public static String getFirstDay()
  {
    Calendar c = Calendar.getInstance();
    c.set(5, 1);
    return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
  }

  public static String getNow()
  {
    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
  }

  public static String getDate() {
    return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  }

  public static String getUrl(String url)
  {
    int aa = url.indexOf("/html");
    if(aa==-1){
    	return url;
    }
    String url2 = url.substring(0, aa + 5);
    url = url.replaceAll(url2, "");
    return url;
  }

  /**
 * 比较ip大小
 * @param startIp
 * @param endIp
 * @return
 */
  public static boolean compare(String startIp,String endIp){ 
	  boolean flag=false; 
	  String startips[]=startIp.split("\\."); 
	  String endIps[]=endIp.split("\\."); 
	  for(int i=0;i<startips.length;i++){ 
	  if(Integer.parseInt(endIps[i])>Integer.parseInt(startips[i])){ 
	  flag=true; 
	  break; 
	  }else{ 
	  if(Integer.parseInt(endIps[i])==Integer.parseInt(startips[i])){ 
	  continue; 
	  }else{ 
	  break; 
	  } 
	  } 
	  } 
	  return flag; 
	  } 

  public static void main(String[] args) {

    System.out.println(compare("192.168.1.99","192.168.1.98"));
  }
}